package org.neowei.image.api;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;

import javax.imageio.ImageIO;

public class ImageOption {
	private String croptype = "";
	private String deeppath = "";
	private String srcpath;
	private String despath;
	private String temppath;
	private String waterpath;
	private String picfilename;
	private String waterfilename;
	private String waterposition = "";
	private int srcwidth;
	private int srcheight;
	private int neowidth;
	private int neoheight;
	private int finalwidth;
	private int finalheight;
	private int cropX;
	private int cropY;
	private int maxwidth;
	private int maxheight;
	private String magickpath;
	private boolean isdelete = false;

	public ImageOption() {
	}

	public ImageOption getNormal(Map<String, String> map) {
		if (!map.isEmpty()) {
			String result = map.get("picfilename");
			if (result != null && result.length() > 0)
				this.picfilename = result;
			result = map.get("deeppath");
			if (result != null && result.length() > 0)
				this.deeppath = result;
		}
		return this;
	}

	public ImageOption getResize(Map<String, String> map) {
		if (!map.isEmpty()) {
			String result = map.get("width");
			if (result != null && result.length() > 0)
				this.neowidth = Integer.valueOf(result);
			result = map.get("height");
			if (result != null && result.length() > 0)
				this.neoheight = Integer.valueOf(result);
			result = map.get("picfilename");
			if (result != null && result.length() > 0)
				this.picfilename = result;
			result = map.get("deeppath");
			if (result != null && result.length() > 0)
				this.deeppath = result;
		}
		return this;
	}

	public ImageOption getCropResize(Map<String, String> map) {
		if (!map.isEmpty()) {
			String result = map.get("width");
			if (result != null && result.length() > 0)
				this.neowidth = Integer.valueOf(result);
			result = map.get("height");
			if (result != null && result.length() > 0)
				this.neoheight = Integer.valueOf(result);
			result = map.get("picfilename");
			if (result != null && result.length() > 0)
				this.picfilename = result;
			result = map.get("deeppath");
			if (result != null && result.length() > 0)
				this.deeppath = result;
		}
		return this;
	}

	public ImageOption getBgresize(Map<String, String> map) {
		if (!map.isEmpty()) {
			String result = map.get("width");
			if (result != null && result.length() > 0)
				this.neowidth = Integer.valueOf(result);
			result = map.get("height");
			if (result != null && result.length() > 0)
				this.neoheight = Integer.valueOf(result);
			result = map.get("picfilename");
			if (result != null && result.length() > 0)
				this.picfilename = result;
			result = map.get("deeppath");
			if (result != null && result.length() > 0)
				this.deeppath = result;
		}
		return this;
	}

	public ImageOption getWater(Map<String, String> map) {
		String result = map.get("width");
		if (result != null && result.length() > 0)
			this.neowidth = Integer.valueOf(result);
		result = map.get("height");
		if (result != null && result.length() > 0)
			this.neoheight = Integer.valueOf(result);
		result = map.get("picfilename");
		if (result != null && result.length() > 0)
			this.picfilename = result;
		result = map.get("deeppath");
		if (result != null && result.length() > 0)
			this.deeppath = result;
		result = map.get("waterfilename");
		if (result != null && result.length() > 0)
			this.waterfilename = result;
		result = map.get("position");
		if (result != null && result.length() > 0)
			this.waterposition = result;
		return this;
	}

	public ImageOption getCrop(Map<String, String> map) {
		if (!map.isEmpty()) {
			String result = map.get("width");
			if (result != null && result.length() > 0)
				this.neowidth = Integer.valueOf(result);
			result = map.get("height");
			if (result != null && result.length() > 0)
				this.neoheight = Integer.valueOf(result);
			result = map.get("picfilename");
			if (result != null && result.length() > 0)
				this.picfilename = result;
			result = map.get("deeppath");
			if (result != null && result.length() > 0)
				this.deeppath = result;
			result = map.get("x");
			if (result != null && result.length() > 0)
				this.cropX = Integer.valueOf(result);
			result = map.get("y");
			if (result != null && result.length() > 0)
				this.cropY = Integer.valueOf(result);
		}
		return this;
	}

	public int getMaxwidth() {
		return maxwidth;
	}

	public void setMaxwidth(int maxwidth) {
		this.maxwidth = maxwidth;
	}

	public int getMaxheight() {
		return maxheight;
	}

	public void setMaxheight(int maxheight) {
		this.maxheight = maxheight;
	}

	public int getSrcwidth() {
		return srcwidth;
	}

	public void setSrcwidth(int srcwidth) {
		this.srcwidth = srcwidth;
	}

	public int getSrcheight() {
		return srcheight;
	}

	public void setSrcheight(int srcheight) {
		this.srcheight = srcheight;
	}

	public String getDeeppath() {
		return deeppath;
	}

	public void setDeeppath(String deeppath) {
		this.deeppath = deeppath;
	}

	public String getSrcpath() {
		return srcpath;
	}

	public void setSrcpath(String srcpath) {
		this.srcpath = srcpath;
	}

	public String getDespath() {
		return despath;
	}

	public void setDespath(String despath) {
		this.despath = despath;
	}

	public String getWaterpath() {
		return waterpath;
	}

	public void setWaterpath(String waterpath) {
		this.waterpath = waterpath;
	}

	public String getPicfilename() {
		return picfilename;
	}

	public void setPicfilename(String picfilename) {
		this.picfilename = picfilename;
	}

	public String getWaterfilename() {
		return waterfilename;
	}

	public void setWaterfilename(String waterfilename) {
		this.waterfilename = waterfilename;
	}

	public int getNeowidth() {
		return neowidth;
	}

	public void setNeowidth(int neowidth) {
		this.neowidth = neowidth;
	}

	public int getNeoheight() {
		return neoheight;
	}

	public void setNeoheight(int neoheight) {
		this.neoheight = neoheight;
	}

	public String getWaterposition() {
		return waterposition;
	}

	public void setWaterposition(String waterposition) {
		this.waterposition = waterposition;
	}

	public int getCropX() {
		return cropX;
	}

	public void setCropX(int cropX) {
		this.cropX = cropX;
	}

	public int getCropY() {
		return cropY;
	}

	public void setCropY(int cropY) {
		this.cropY = cropY;
	}

	public String getCroptype() {
		return croptype;
	}

	public void setCroptype(String croptype) {
		this.croptype = croptype;
	}

	public String getTemppath() {
		return temppath;
	}

	public void setTemppath(String temppath) {
		this.temppath = temppath;
	}

	public boolean isIsdelete() {
		if (this.neoheight > this.maxheight || this.neowidth > this.maxwidth) {
			this.isdelete = true;
		}
		if (this.neowidth % 5 != 0 || this.neoheight % 5 != 0 || this.neowidth == 0 || this.neoheight == 0) {
			this.isdelete = true;
		}
		return isdelete;
	}

	public String getMagickpath() {
		return magickpath;
	}

	public void setMagickpath(String magickpath) {
		this.magickpath = magickpath;
	}

	public int getFinalwidth() {
		return finalwidth;
	}

	public void setFinalwidth(int finalwidth) {
		this.finalwidth = finalwidth;
	}

	public int getFinalheight() {
		return finalheight;
	}

	public void setFinalheight(int finalheight) {
		this.finalheight = finalheight;
	}

	public void setBGFinalWH() {
		boolean widthzero = this.neowidth == 0;
		boolean heightzero = this.neoheight == 0;
		double finalw;
		double finalh;
		if (widthzero && heightzero) {
			finalw = 0.0;
			finalh = 0.0;
		} else if (widthzero) {
			finalh = this.neoheight;
			finalw = finalh * this.srcwidth / this.srcheight;
		} else if (heightzero) {
			finalw = this.neowidth;
			finalh = finalw * this.srcheight / this.srcwidth;
		} else {
			finalw = neowidth;
			finalh = this.srcheight * this.neowidth / this.srcwidth;
			double neoarea = this.neowidth * this.neoheight;
			if (finalw * finalh > neoarea) {
				finalh = this.neoheight;
				finalw = this.srcwidth * this.neoheight / this.srcheight;
			}
		}
		this.finalwidth = (int) finalw;
		this.finalheight = (int) finalh;
	}

	public double getLanczos(int i, double inWidth, double outWidth) {
		double x = i * outWidth / inWidth;
		return Math.sin(x * StrictMath.PI) / (x * StrictMath.PI) * Math.sin(x * StrictMath.PI / 3) / (x * StrictMath.PI / 3);
	}

	@Override
	public String toString() {
		return "ImageOption [croptype=" + croptype + ", deeppath=" + deeppath + ", srcpath=" + srcpath + ", despath=" + despath + ", temppath=" + temppath + ", waterpath=" + waterpath
				+ ", picfilename=" + picfilename + ", waterfilename=" + waterfilename + ", waterposition=" + waterposition + ", srcwidth=" + srcwidth + ", srcheight=" + srcheight + ", neowidth="
				+ neowidth + ", neoheight=" + neoheight + ", cropX=" + cropX + ", cropY=" + cropY + ", magickpath=" + magickpath + ", isdelete=" + isdelete + "]";
	}

	public String getSrcpathWithFilename() {
		return new StringBuffer(this.getSrcpath()).append(File.separatorChar).append(this.getDeeppath()).append(File.separatorChar).append(this.getPicfilename()).toString();
	}

	public String getDespathWithFilename() {
		StringBuffer desPathWithFilename = new StringBuffer();
		if (null != this.temppath && this.temppath.length() > 0 && this.isIsdelete() == true) {
			desPathWithFilename.append(this.temppath).append(File.separatorChar).append(this.picfilename);
			return desPathWithFilename.toString();
		} else {
			desPathWithFilename.append(this.despath);
		}
		desPathWithFilename.append(File.separatorChar).append(this.croptype);
		desPathWithFilename.append(File.separatorChar).append(this.neowidth);
		desPathWithFilename.append(File.separatorChar).append(this.neoheight);
		desPathWithFilename.append(File.separatorChar).append(this.deeppath);
		desPathWithFilename.append(File.separatorChar).append(this.waterposition);
		if (this.getCropX() != 0)
			desPathWithFilename.append(File.separatorChar).append(this.cropX);
		if (this.getCropY() != 0)
			desPathWithFilename.append(File.separatorChar).append(this.cropY);
		File file = new File(desPathWithFilename.toString());
		if (!file.exists()) {
			file.mkdirs();
		}
		desPathWithFilename.append(File.separatorChar).append(this.picfilename);
		return desPathWithFilename.toString();
	}

	public void setSrcWidthHeight() {
		try {
			BufferedImage bi = ImageIO.read(new FileInputStream(this.getSrcpathWithFilename()));
			this.srcwidth = bi.getWidth();
			this.srcheight = bi.getHeight();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
